<?php
$session_name = session_name();
if (!isset($_POST[$session_name])) {
    exit;
} else {
    session_id($_POST[$session_name]);
    session_start();
}
//判断是否登录
if(!isset($_SESSION['user'])){
	exit;
}
//引入公共定义文件
require_once(str_replace("/",DIRECTORY_SEPARATOR,"../../../../../config/define.php"));
//已获取到session
$uploadType = 'file';
//按文件夹上传
if(isset($_POST['isImg'])&&$_POST['isImg']=='1') $uploadType = 'image';
$relativePath = DS.'data'.DS.'ueditor'.DS.'upload'.DS.$uploadType.DS.date('Ymd');
$targetFolder = $_SERVER['DOCUMENT_ROOT'].$relativePath;

function mkdirs($dir)  {
	 if(!is_dir($dir))  {
	  if(!mkdirs(dirname($dir))){ 
	   return false;  
	  } 
	  if(!mkdir($dir,0777)){ 
	   return false;  
	  } 
	 }  
	 return true;  
}
//自动创建文件夹
mkdirs($targetFolder);
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath =  $targetFolder;
	$targetFile = rtrim($targetPath,'/') . DS. $_FILES['Filedata']['name'];
	// 检验上传文件类型
	$fileTypes  = explode(',',UPLOAD_TYPES);
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	if (in_array(strtolower($fileParts['extension']),$fileTypes)) {
		move_uploaded_file($tempFile,$targetFile);
		echo str_replace(DS,"/",$relativePath.DS. $_FILES['Filedata']['name']);
	} else {
		echo 'Invalid file type.';
	}
}else{
	echo 'Invalid file Validate.';
}
?>